package com.shuoyue.ycgk.ui.shop;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.shuoyue.ycgk.R;
import com.shuoyue.ycgk.apputis.GlideImageLoader;
import com.shuoyue.ycgk.base.BaseMvpFragment;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.MyBanner;
import com.shuoyue.ycgk.entity.ShopItem;
import com.shuoyue.ycgk.entity.Type;
import com.shuoyue.ycgk.ui.course.FragmengCourseItem;
import com.shuoyue.ycgk.ui.shop.contract.ShopGoodsContract;
import com.shuoyue.ycgk.utils.GridSpacingItemDecoration;
import com.shuoyue.ycgk.utils.SizeUtil;
import com.shuoyue.ycgk.utils.SpacesItemDecoration;
import com.shuoyue.ycgk.views.PageIndicator;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class FragmentShopItem extends BaseMvpFragment<ShopGoodsContract.Presenter> implements ShopGoodsContract.View {

    @BindView(R.id.shopRecycleView)
    RecyclerView shopRecycleView;
    @BindView(R.id.refreshlayout)
    SmartRefreshLayout refreshlayout;
    ListWithPage listWithPage;
    @BindView(R.id.home_banner)
    Banner homeBanner;
    @BindView(R.id.pageindicatorBanner)
    PageIndicator pageindicatorBanner;
    @BindView(R.id.lay_banner)
    CardView layBanner;


    ShopAdapter shopAdapter;
    ShopAdapterHorizontal shopAdapterHorizontal;
    Type baseType;
    String title;


    @Override
    public void initParams(Bundle bundle) {
        baseType = (Type) bundle.getSerializable("questionType");
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_main_shop_item;
    }

    @Override
    protected void initView() {
//        shopRecycleView.setLayoutManager(new LinearLayoutManager(mContext));
//        shopRecycleView.addItemDecoration(new SpacesItemDecoration(SizeUtil.dip2px(mContext, 8), false));
        shopRecycleView.setLayoutManager(new GridLayoutManager(mContext,2));
        shopRecycleView.addItemDecoration(new GridSpacingItemDecoration(2, SizeUtil.dip2px(mContext, 8), false));

        shopAdapter = new ShopAdapter(null);
        shopRecycleView.setAdapter(shopAdapter);
        shopAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            ShopItem shopItem = (ShopItem) adapter.getItem(position);
            if (shopItem.getStatus() == 1) {
                startActivity(new Intent(mContext, ShopInfoActivity.class)
                        .putExtra("id", shopItem.getId()));
            } else {
                toast("图书正在上架，请关注上架后购买");
            }
        });
        registEmptyView(shopAdapter);

        refreshlayout.setOnRefreshListener(refreshLayout -> {
            mPresenter.getBookList(baseType.getId(), 1);
            refreshLayout.finishRefresh(2000);
        });
        refreshlayout.setOnLoadMoreListener(refreshLayout -> {
            mPresenter.getBookList(baseType.getId(), listWithPage.getCurrent() + 1);
            refreshLayout.finishLoadMore(2000);
        });


    }

    @Override
    protected void initData() {
        mPresenter = new ShopGoodsContract.Presenter();
        mPresenter.attachView(this);
        mPresenter.getBookList(baseType.getId(), 1);
        mPresenter.getBanners();

    }

    public static FragmentShopItem getInstance(Type baseType) {
        FragmentShopItem fragment = new FragmentShopItem();
        fragment.title = baseType.getName();
        Bundle bundle = new Bundle();
        bundle.putSerializable("questionType", baseType);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public String getTitle() {
        return title;
    }


    @Override
    public void setBaseType(List<Type> baseTypes) {

    }

    @Override
    public void setBooksList(ListWithPage<ShopItem> booksList) {
        listWithPage = booksList;
        if (booksList.getCurrent() == 1) {
            shopAdapter.resetData(booksList.getRecords());
        } else {
            shopAdapter.addData(booksList.getRecords());
        }
        refreshlayout.setEnableLoadMore(booksList.getCurrent() != booksList.getPages() && booksList.getPages() > 0);
        refreshlayout.finishRefresh();
        refreshlayout.finishLoadMore();
    }

    @Override
    public void setBanners(ArrayList<MyBanner> banners) {
        setmBannerView(banners);
    }


    /**
     * 设置banner信息
     *
     * @param data
     */
    public void setmBannerView(ArrayList<MyBanner> data) {
        layBanner.setVisibility(data == null || data.size() == 0 ? View.GONE : View.VISIBLE);
        List<String> iBanners = new ArrayList<>();
        for (MyBanner banner : data) {
            iBanners.add(banner.getImgUrl());
        }
        //设置banner样式
        homeBanner.setBannerStyle(BannerConfig.NOT_INDICATOR);
        //设置图片加载器
        homeBanner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        homeBanner.setImages(iBanners);
        //设置banner动画效果
        homeBanner.setBannerAnimation(Transformer.Default);
//            //设置标题集合（当banner样式有显示title时）
        homeBanner.setBannerTitles(iBanners);
        //设置自动轮播，默认为true
        homeBanner.isAutoPlay(true);
        //设置轮播时间
        homeBanner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）
        homeBanner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        homeBanner.start();
//            homeBanner.setBannerView(iBanners).setImageCorner(18).start();
        pageindicatorBanner.setNumPages(iBanners.size());
        pageindicatorBanner.setCurrentPage(0);

        homeBanner.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (pageindicatorBanner != null) pageindicatorBanner.setCurrentPage(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        homeBanner.setOnBannerListener(position -> {
        });
    }

}
